<?php

include_model("ModulesDao");

class ModulesHandler extends admCtrl{
    
    /**
     * 
     * @var ModulesDao
     */
    private $mDao;
    
    
	/**
	 * 
	 * @param Request $req
	 * @param TimeMachine $delorean
	 * @param PDIO $pdio
	 */
	public function __construct($req, $delorean, $pdio) {
		parent::__construct($req, $delorean, $pdio);
		$this->mDao = new ModulesDao($pdio);
		
	}

    public function index() {
        
        global $ServiceHandler;

        $viMods = $ServiceHandler->call('Modules_InstalledList');
        $niMods = $ServiceHandler->call('Modules_NotInstalledList');

        $this->response->set('viMods', $viMods);
        $this->response->set('niMods', $niMods);
        $this->response->set('debug', print_r(array('vi' => $viMods, 'ni' => $niMods), true));
        
        $this->response->set('instURL', ROOT_ADDR.'index.php?ctrl=ModulesHandler&act=install&modname=');
        
        $this->response->setTpl('ModulesHandler/modulesHandlerIndex.html');
        
    }
    
    
    public function install() {
        $modname = $this->request->get('modname');
        
        require_once APP_PATH."modules/$modname/install.php";
        if (install($this->pdio)) {
    		$modules = $this->mDao->install($modname);
            $esito = 'OK';
        } else {
            $esito = "installazione $modname fallita";
        }
        
        $this->response->set('esito', $esito);
        $this->response->setTpl('ModulesHandler/esitoInstallazione.html');        
    }
    
    public function uninstall() {
        $modname = $this->request->get('modname');
    
        require_once APP_PATH."modules/$modname/install.php";
        if (uninstall($this->pdio)) {
            $modules = $this->mDao->uninstall($modname);
            $esito = 'disintallazione OK';
        } else {
            $esito = "disinstallazione $modname fallita";
        }
    
        $this->response->set('esito', $esito);
        $this->response->setTpl('ModulesHandler/esitoInstallazione.html');
    }    
    
    
    
    private static function include_module($modname) {
        include_once(MODULES_PATH.$modname.'/ctrl/'.$modname.'php');
    }


}